Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to convert text files to HTML files?

0 views
Skip to first unread message

Suhas

unread,
Dec 12, 2002, 4:10:36 PM12/12/02
to
Hi Everyone,

How can I convert text (ASCII) files to HTML files/documents using
UNIX shell scripting. I know there are utilities out there that use
perl, etc., but was wondering if anyone has used shell scripting.

Thanks in advance.
Suhas

Chris F.A. Johnson

unread,
Dec 12, 2002, 4:29:08 PM12/12/02
to

The simplest:

{ echo "<pre>"
cat "$@"
echo "</pre>"
} > text.html


To convert a text file with paragraphs separated by blank lines:

{
echo "<html><head>
<title>$title</title></head>
<body>
<p>"

sed 's/^$/<p>/' "$@"
echo "</body></html>
} > text.html


You can do whatever you want with a shell script.

--
Chris F.A. Johnson http://cfaj.freeshell.org
===================================================================
My code (if any) in this post is copyright 2002, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License

William Park

unread,
Dec 12, 2002, 4:42:59 PM12/12/02
to
Suhas <sgt...@hotmail.com> wrote:
> Hi Everyone,
>
> How can I convert text (ASCII) files to HTML files/documents using
> UNIX shell scripting. I know there are utilities out there that use
> perl, etc., but was wondering if anyone has used shell scripting.

You can't. You are trying to get an egg from omelette.

--
William Park, Open Geometry Consulting, <openge...@yahoo.ca>
Linux solution for data management and processing.

Chris F.A. Johnson

unread,
Dec 12, 2002, 4:49:13 PM12/12/02
to
On Thu, 12 Dec 2002 at 21:42 GMT, William Park wrote:
> Suhas <sgt...@hotmail.com> wrote:
>> Hi Everyone,
>>
>> How can I convert text (ASCII) files to HTML files/documents using
>> UNIX shell scripting. I know there are utilities out there that use
>> perl, etc., but was wondering if anyone has used shell scripting.
>
> You can't. You are trying to get an egg from omelette.

You mean I'm imagining the dozens of scripts I've written to do
just that?

William Park

unread,
Dec 12, 2002, 6:36:06 PM12/12/02
to
Chris F.A. Johnson <c.f.a....@rogers.com> wrote:
> On Thu, 12 Dec 2002 at 21:42 GMT, William Park wrote:
>> Suhas <sgt...@hotmail.com> wrote:
>>> Hi Everyone,
>>>
>>> How can I convert text (ASCII) files to HTML files/documents using
>>> UNIX shell scripting. I know there are utilities out there that use
>>> perl, etc., but was wondering if anyone has used shell scripting.
>>
>> You can't. You are trying to get an egg from omelette.
>
> You mean I'm imagining the dozens of scripts I've written to do
> just that?

You get pieces of cooked egg. OP wants the whole eggs in its own shell.

Bill Marcum

unread,
Dec 12, 2002, 7:29:03 PM12/12/02
to
On 12 Dec 2002 21:42:59 GMT,
William Park <openge...@yahoo.ca> wrote:
> Suhas <sgt...@hotmail.com> wrote:
>> Hi Everyone,
>>
>> How can I convert text (ASCII) files to HTML files/documents using
>> UNIX shell scripting. I know there are utilities out there that use
>> perl, etc., but was wondering if anyone has used shell scripting.
>
> You can't. You are trying to get an egg from omelette.
>
Are you reading the original poster's mind to determine that the text
file was once HTML?

K, Jaiganesh [RICH2:MX00:EXCH]

unread,
Dec 12, 2002, 8:57:03 PM12/12/02
to
---------------------------------------------
#!/usr/bin/awk -f

BEGIN {
printf("<html>\n");
printf("<title> %s </title>\n", FILENAME);
printf("<body text=\"#000000\" bgcolor=\"#FFFFFF\" link=\"#0000EE\"
vlink=\"#990000\" alink=\"#FE0000\">");
}

{ printf("%s<BR>\n",$0); }

END {

printf("</body>\n");
printf("</html>\n");


}

----------------------------------------------------------------
"Suhas" <sgt...@hotmail.com> wrote in message
news:5c440ee2.02121...@posting.google.com...

Peter J. Acklam

unread,
Dec 13, 2002, 1:51:15 PM12/13/02
to
"Chris F.A. Johnson" <c.f.a....@rogers.com> wrote:

> Suhas wrote:
> >
> > How can I convert text (ASCII) files to HTML files/documents using
> > UNIX shell scripting. I know there are utilities out there that use
> > perl, etc., but was wondering if anyone has used shell scripting.
>
> The simplest:
>
> { echo "<pre>"
> cat "$@"
> echo "</pre>"
> } > text.html

Even the simplest text to HTML converter needs to "escape" the
characters "&", "<", and ">" to prevent them from being
interpreted. :-)

My attempt...

#!/bin/sh

echo "<pre>"
sed 's/&/\&amp;/g ; s/</\&lt;/g ; s/>/\&gt;/g' "$@"
echo "</pre>"

Peter

--
"If I haven't seen further it is because giants have
been standing in my way." -- Peter J. Acklam

Randal L. Schwartz

unread,
Dec 22, 2002, 2:41:14 PM12/22/02
to Peter J. Acklam
>>>>> "Peter" == Peter J Acklam <pjac...@online.no> writes:

Peter> My attempt...

Peter> #!/bin/sh

Peter> echo "<pre>"
Peter> sed 's/&/\&amp;/g ; s/</\&lt;/g ; s/>/\&gt;/g' "$@"
Peter> echo "</pre>"

But that's not using "the shell". That's using "sed".
If you're gonna use sed, you might as well use Perl.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<mer...@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

Chris F.A. Johnson

unread,
Dec 22, 2002, 7:18:50 PM12/22/02
to
On Sun, 22 Dec 2002 at 19:41 GMT, Randal L. Schwartz wrote:
>>>>>> "Peter" == Peter J Acklam <pjac...@online.no> writes:
>
>Peter> My attempt...
>
>Peter> #!/bin/sh
>
>Peter> echo "<pre>"
>Peter> sed 's/&/\&amp;/g ; s/</\&lt;/g ; s/>/\&gt;/g' "$@"
>Peter> echo "</pre>"
>
> But that's not using "the shell". That's using "sed".
> If you're gonna use sed, you might as well use Perl.

That's like answering: "It's two far to walk, I'll take the car."
with "If you're not going to walk, you might as well drive an
18-wheeler."

Peter J. Acklam

unread,
Dec 23, 2002, 12:09:10 PM12/23/02
to
mer...@stonehenge.com (Randal L. Schwartz) wrote:

>>>>>> "Peter" == Peter J Acklam <pjac...@online.no> writes:
>
> Peter> My attempt...
>
> Peter> #!/bin/sh
>
> Peter> echo "<pre>"
> Peter> sed 's/&/\&amp;/g ; s/</\&lt;/g ; s/>/\&gt;/g' "$@"
> Peter> echo "</pre>"
>
> But that's not using "the shell". That's using "sed".
> If you're gonna use sed, you might as well use Perl.

Sure, but this being comp.unix.shell, I post shell scripts unless
there is a real benefit from using perl.

Besides, there are more computers with "sed" installed than with
"perl" installed.

Peter

--
I wish dialog boxes had a butten saying "Whatever". I hate being
forced to answer "Yes" or "No" to a question I have no opinion on
whatsoever. There ought to be a button matching my indifference.

Peter J. Acklam

unread,
Dec 23, 2002, 12:17:09 PM12/23/02
to
"Chris F.A. Johnson" <c.f.a....@rogers.com> wrote:

> Randal L. Schwartz wrote:
>
> > But that's not using "the shell". That's using "sed".
> > If you're gonna use sed, you might as well use Perl.
>
> That's like answering: "It's two far to walk, I'll take the
> car." with "If you're not going to walk, you might as well
> drive an 18-wheeler."

I see your point, but considering that perl is both faster and
more flexible than the shell, I'm not sure if "18-wheeler" vs
"car" is a good analogy for "perl" vs "shell". :-)

Sven Mascheck

unread,
Dec 23, 2002, 1:18:43 PM12/23/02
to
Randal L. Schwartz wrote:

> But that's not using "the shell". That's using "sed".
> If you're gonna use sed, you might as well use Perl.

Huh - c'mon, surely you know better:

Utilities which are installed by tradition and which are very
useful in shell scripts, are on-topic here. Let alone awk(1)...

perl(1) in contrast has its own group,
so what's your (biased) point? ;-)

Sven

Chris F.A. Johnson

unread,
Dec 24, 2002, 4:17:17 PM12/24/02
to
On Mon, 23 Dec 2002 at 17:17 GMT, Peter J. Acklam wrote:
> "Chris F.A. Johnson" <c.f.a....@rogers.com> wrote:
>
>> Randal L. Schwartz wrote:
>>
>> > But that's not using "the shell". That's using "sed".
>> > If you're gonna use sed, you might as well use Perl.
>>
>> That's like answering: "It's two far to walk, I'll take the
>> car." with "If you're not going to walk, you might as well
>> drive an 18-wheeler."
>
> I see your point, but considering that perl is both faster and
> more flexible than the shell, I'm not sure if "18-wheeler" vs
> "car" is a good analogy for "perl" vs "shell". :-)

If speed is an issue, I'll use C, which is several orders of
magnitude more efficient than Perl.

Peter J. Acklam

unread,
Dec 25, 2002, 10:23:50 AM12/25/02
to
"Chris F.A. Johnson" <c.f.a....@rogers.com> wrote:

> Peter J. Acklam wrote:
>
> > [...] considering that perl is both faster and more flexible


> > than the shell, I'm not sure if "18-wheeler" vs "car" is a
> > good analogy for "perl" vs "shell". :-)
>
> If speed is an issue, I'll use C, which is several orders of
> magnitude more efficient than Perl.

The speed factor depends entirely on what you are doing.

Randal L. Schwartz

unread,
Dec 25, 2002, 12:37:58 PM12/25/02
to
>>>>> "Chris" == Chris F A Johnson <c.f.a....@rogers.com> writes:

Chris> If speed is an issue, I'll use C, which is several orders of
Chris> magnitude more efficient than Perl.

Maybe for benchmarks you select. Overall, "several orders of
magnitude" is clearly a lie. We've got "typical mix" benchmarks that
show that Perl is even faster than Java for a general e-commerce
system.

Besides, these days, it's I/O that matters. Not CPU, unless you're
simulating weather or a nuclear explosion.

Randal L. Schwartz

unread,
Dec 25, 2002, 12:39:07 PM12/25/02
to
>>>>> "Sven" == Sven Mascheck <sven.m...@student.uni-ulm.de> writes:

Sven> Randal L. Schwartz wrote:
>> But that's not using "the shell". That's using "sed".
>> If you're gonna use sed, you might as well use Perl.

Sven> Huh - c'mon, surely you know better:

Sven> Utilities which are installed by tradition and which are very
Sven> useful in shell scripts, are on-topic here. Let alone awk(1)...

I bet 80% or more of the "unix" installations have /usr/bin/perl
or /usr/local/bin/perl.

Counting Linux, of course. Which is the "new" tradition.

Chris F.A. Johnson

unread,
Dec 25, 2002, 7:26:32 PM12/25/02
to
On Wed, 25 Dec 2002 at 17:37 GMT, Randal L. Schwartz wrote:
>>>>>> "Chris" == Chris F A Johnson <c.f.a....@rogers.com> writes:
>
>Chris> If speed is an issue, I'll use C, which is several orders of
>Chris> magnitude more efficient than Perl.
>
> Maybe for benchmarks you select. Overall, "several orders of
> magnitude" is clearly a lie. We've got "typical mix" benchmarks that
> show that Perl is even faster than Java for a general e-commerce
> system.

I wasn't referring to speed, but to usage of resources, primarily
memory.

> Besides, these days, it's I/O that matters. Not CPU, unless you're
> simulating weather or a nuclear explosion.

--

William Park

unread,
Dec 26, 2002, 9:30:28 PM12/26/02
to
Chris F.A. Johnson <c.f.a....@rogers.com> wrote:
> On Wed, 25 Dec 2002 at 17:37 GMT, Randal L. Schwartz wrote:
>>>>>>> "Chris" == Chris F A Johnson <c.f.a....@rogers.com> writes:
>>
>>Chris> If speed is an issue, I'll use C, which is several orders of
>>Chris> magnitude more efficient than Perl.
>>
>> Maybe for benchmarks you select. Overall, "several orders of
>> magnitude" is clearly a lie. We've got "typical mix" benchmarks that
>> show that Perl is even faster than Java for a general e-commerce
>> system.
>
> I wasn't referring to speed, but to usage of resources, primarily
> memory.

Chris, you're being sucked into a useless flame war. Those who make
income from Perl will never admit their flaky position. Just like I'll
never admit Python can do any wrong. :-)

William Park

unread,
Dec 26, 2002, 9:31:54 PM12/26/02
to
Randal L. Schwartz <mer...@stonehenge.com> wrote:
>>>>>> "Sven" == Sven Mascheck <sven.m...@student.uni-ulm.de> writes:
>
> Sven> Utilities which are installed by tradition and which are very
> Sven> useful in shell scripts, are on-topic here. Let alone awk(1)...
>
> I bet 80% or more of the "unix" installations have /usr/bin/perl
> or /usr/local/bin/perl.

100% of "Unix" have Awk.

Peter J. Acklam

unread,
Dec 27, 2002, 6:44:37 AM12/27/02
to
William Park <openge...@yahoo.ca> wrote:

> Chris F.A. Johnson <c.f.a....@rogers.com> wrote:
>
> > I wasn't referring to speed, but to usage of resources,
> > primarily memory.
>
> Chris, you're being sucked into a useless flame war.

Considering what Chris has written in this thread, I consider him
one of the war mongers. :-)

> Those who make income from Perl will never admit their flaky
> position. Just like I'll never admit Python can do any wrong.
> :-)

I think this is being narrow-minded. I use Perl a lot, but I have
no problems admitting that Perl has it's weaknesses and
limitations -- just like *any* other tool or programming language.

In the case of converting text to HTML, which this thread was
about, sed was enough. In other cases, I'll use awk or Perl.

Chris F.A. Johnson

unread,
Dec 29, 2002, 4:36:08 AM12/29/02
to
On Fri, 27 Dec 2002 at 11:44 GMT, Peter J. Acklam wrote:
> William Park <openge...@yahoo.ca> wrote:
>
>> Chris F.A. Johnson <c.f.a....@rogers.com> wrote:
>>
>> > I wasn't referring to speed, but to usage of resources,
>> > primarily memory.
>>
>> Chris, you're being sucked into a useless flame war.
>
> Considering what Chris has written in this thread, I consider him
> one of the war mongers. :-)

Guilty as charged. :(

Though that was not my intention.

0 new messages